﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasCityDAL : IMasCityDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasCityDAL()
        {         
        }
        public DataSet LoadMasCities(MasCityInfo cityInfo)
        {
            string strSQL;
            Nullable<int> countSeq = cityInfo.Countries == null ? 0 : cityInfo.Countries.Sequence;
            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", cityInfo.Sequence);
                dbManager.AddParameters(2, "@CountrySeq", countSeq);

                strSQL = "SELECT c.*, cc.COUNTRY_NAME"
                     + " FROM MAS_CITIES c"
                     + " LEFT JOIN MAS_COUNTRIES cc ON c.COUNTRY_SEQ = cc.COUNTRY_SEQ"
                     + " WHERE c.RECORD_STATUS = @ActiveRecord"
                     + (cityInfo.Sequence > 0 ? " AND c.CITY_SEQ = @Sequence " : "")
                     + (countSeq > 0 ? " AND c.COUNTRY_SEQ = @CountrySeq " : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                return ds;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public IList<MasCityInfo> ListMasCities(MasCityInfo cityInfo)
        {
            string strSQL;
            Nullable<int> countSeq = cityInfo.Countries == null ? 0 : cityInfo.Countries.Sequence;
            List<MasCityInfo> infoList = new List<MasCityInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", cityInfo.Sequence);
                dbManager.AddParameters(2, "@CountrySeq", countSeq);

                strSQL = "SELECT CITY_SEQ, CITY_NAME, CITY_NAME_ENG, COUNTRY_SEQ, REMARKS, CREATED_BY, UPDATED_BY"
                     + " FROM MAS_CITIES"
                     + " WHERE RECORD_STATUS not like @DeleteRecord"
                     + (cityInfo.Sequence > 0 ? " AND CITY_SEQ = @Sequence " : "")
                     + (countSeq > 0 ? " AND COUNTRY_SEQ = @CountrySeq " : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    int ctrSeq = (rdr.IsDBNull(3)) ? 0 : rdr.GetInt32(3);
                    MasCountryDAL countryDAL = new MasCountryDAL();
                    MasCountryInfo ctrInfo = new MasCountryInfo();
                    IList<MasCountryInfo> countryList = countryDAL.ListMasCountries(new MasCountryInfo(ctrSeq));
                    if (countryList.Count > 0)
                        ctrInfo = countryList[0];

                    MasCityInfo Info = new MasCityInfo(rdr.GetInt32(0)
                        , (rdr.IsDBNull(1)) ? "" : rdr.GetString(1)
                        , (rdr.IsDBNull(2)) ? "" : rdr.GetString(2)
                        , ctrInfo
                        , (rdr.IsDBNull(4)) ? "" : rdr.GetString(4)
                        , 0, 0);

                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Insert(MasCityInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Name", Info.Name);
                dbManager.AddParameters(1, "@NameEng", Info.NameEng);
                dbManager.AddParameters(2, "@CountrySeq", Info.Countries.Sequence);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(6, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_CITIES(CITY_NAME, CITY_NAME_ENG, COUNTRY_SEQ"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @NameEng, @CountrySeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(MasCityInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@CountrySeq", Info.Countries.Sequence);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_CITIES"
                           + " SET CITY_NAME = @Name"
                                + ", CITY_NAME_ENG = @NameEng"
                                + ", COUNTRY_SEQ = @CountrySeq"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE CITY_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Seq, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Seq);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_CITIES"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE CITY_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
